<?php
namespace app\admin\controller\advertisement;;

use app\admin\model\AdvertisementType;
use app\common\controller\AdminController;
use EasyAdmin\annotation\ControllerAnnotation;
use EasyAdmin\annotation\NodeAnotation;
use app\admin\model\publicuse\PublicUse;
use think\App;
/**
 * @ControllerAnnotation(title="广告")
 * Class Adve
 */
class Adve extends AdminController
{
    use \app\admin\traits\Curd;
     protected $allowModifyFields = [
        'status',
    ];
    protected $noExportFields = [
        'id',
        'name',
        'sort',
        'identification',
        'img',
        'type_id',
        'href',
        'explain',
        'create_time',
        'update_time',
        'status',
        'delete_time',
    ];
    public function __construct(App $app)
    {
        parent::__construct($app);
        $this->model = new \app\admin\model\Advertisement();
        $array = PublicUse::getConfigDir(__CLASS__);
        if(is_file(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt')){
           $this->AllData = json_decode(file_get_contents(__DIR__.DIRECTORY_SEPARATOR.'field'.DIRECTORY_SEPARATOR.$array['class'].'.txt'),true);
        }
    }
    public function add()
    {
        $data = request()->param();
        if ($this->request->isAjax()) {
            $post = $this->request->post();
//            return $this->error('不从',$this->AllData);
            $rule = [];
            $this->validate($post, $rule);
//            return $this->error('保存失败2',$post);
            if(isset($this->AllData['field'])&&!empty($this->AllData['field'])){
                $post = PublicUse::Conversion($post,$this->AllData['field']);
            }
//            return $this->error('保存失败1',$post);
            try {
                $save = $this->model->save($post);
            } catch (\Exception $e) {
                $this->error('保存失败:'.$e->getMessage());
            }
            $save ? $this->success('保存成功') : $this->error('保存失败');
        }
        if(isset($this->AllData['field'])){
            if($this->AllData['field']){
                $this->AllData['field'] = PublicUse::FieldGetLianJie($this->AllData['field']);
            }
//            echo '<pre>';
//            print_r($this->AllData['field']);
//            exit;
            if($this->AllData['tabletype']=='tree'){
                if(isset($this->AllData['field']['pid']['option'])&&$this->AllData['field']['pid']['option']){
                    $this->AllData['field']['pid']['option'] = PublicUse::getPidMenuList($this->AllData['field']['pid']['option']);
                }
            }
            $this->assign('alldata',$this->AllData);
        }
        if(isset($data['field'])&&isset($data['id'])&&$data['field']&&$data['id']){
            $data[$data['field']] = $data['id'];
            unset($data['field']);
            unset($data['id']);
        }
        $type = AdvertisementType::find($data['type_id']);
        $data['identification'] = $type->identification;
//        echo '<pre>';
//        print_r($data);
//        exit;
        $this->assign('row',$data);
        return $this->fetch();
    }
}